package com.mnt.gui.base.launcher;

import java.util.ArrayList;
import java.util.List;

import com.mnt.gui.base.controller.BaseController;
import com.mnt.gui.base.util.FXMLLoaderTool;

/**
 * page controller
 * control page jump
 * @author mnt.cico
 *
 */
public class PageController {
	private static final List<BaseController> controllerList = new ArrayList<BaseController>();
	private static int currIndex = 0;
	private PageController() {
		
	}
	
	/**
	 * view jump to base controller view
	 * @param controller
	 */
	public static final <C extends BaseController>void to(Class<C> controller) {
		BaseController baseController = FXMLLoaderTool.load(controller);
		BaseLauncher.setRoot(baseController);
		controllerList.add(baseController);
		currIndex = controllerList.size() - 1;
		baseController.showAndExecute();
	}
	
	/**
	 * back to the last view
	 */
	public static final void back() {
		currIndex = currIndex - 1;
		if(currIndex >= 0) {
			BaseLauncher.setRoot(controllerList.get(currIndex));
		}
	}
	
	/**
	 * forward to next view
	 */
	public static final void forward() {
		currIndex = currIndex - 1;
		if(currIndex < controllerList.size()) {
			BaseLauncher.setRoot(controllerList.get(currIndex));
		}
	}
	
	/**
	 * set first controller
	 * @param baseController
	 */
	public static final void addIndexController(BaseController baseController) {
		controllerList.add(0, baseController);
	}
	
	public static final int getCurrIndex() {
		return currIndex;
	}
}
